package com.dk.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dk.mapper.RoleMenuMapper;
import com.dk.mapper.TRoleMapper;
import com.dk.pojo.RoleMenuExample;
import com.dk.pojo.TRole;
import com.dk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
@org.springframework.stereotype.Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private TRoleMapper tRoleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Override
    public List<TRole> getinfo() {
        List<TRole> list = tRoleMapper.selectByExample(null);

        return list;
    }

    @Override
    public boolean deleteInfo(Integer roleId) {
        try {
            tRoleMapper.deleteByPrimaryKey(roleId);
            RoleMenuExample rme = new RoleMenuExample();
            RoleMenuExample.Criteria criteria = rme.createCriteria();
            criteria.andRoleIdEqualTo(roleId);
            roleMenuMapper.deleteByExample(rme);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public boolean addinfo(TRole role, Integer[] nodeIds) {
        try {
            tRoleMapper.insertRole(role);
            Integer roleId = role.getRoleId();
            roleMenuMapper.insertInfo(roleId,nodeIds);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public boolean updateInfo(Integer[] nodeIds, TRole tRole) {
        try {
            //修改角色信息
            tRoleMapper.updateByPrimaryKeySelective(tRole);
            //通过Rid删除相对应的所有全限信息
            RoleMenuExample rme = new RoleMenuExample();
            RoleMenuExample.Criteria criteria = rme.createCriteria();
            criteria.andRoleIdEqualTo(tRole.getRoleId());
            roleMenuMapper.deleteByExample(rme);
            //增加相对应的角色
            Integer rid = tRole.getRoleId();
            roleMenuMapper.insertInfo(rid,nodeIds);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public TRole getInfo(Integer id) {

        return tRoleMapper.selectByPrimaryKey(id);
    }
}
